package com.zhu.gmallpublisher.controller;

import com.zhu.gmallpublisher.bean.*;
import com.zhu.gmallpublisher.service.TrafficChannelStatsService;
import com.zhu.gmallpublisher.service.TrafficKeywordService;
import com.zhu.gmallpublisher.service.TrafficVisitorStatsService;
import com.zhu.gmallpublisher.utils.GetDateUtil;
import com.zhu.gmallpublisher.utils.GMVJsonUtil;
import com.zhu.gmallpublisher.utils.TrafficStatsJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.*;

/**
 * 用户访问域 控制层
 */

@RestController
@RequestMapping("/zhu/gmall/sugar")
public class TrafficController {

    @Autowired
    private TrafficChannelStatsService trafficChannelStatsService;

    @Autowired
    private TrafficVisitorStatsService trafficVisitorStatsService;

    @Autowired
    private TrafficKeywordService trafficKeywordService;


    /**
     * 渠道日活
     * @param selectDate
     * @return
     */
    @RequestMapping("/uv")
    public String getUvByCh(@RequestParam(value = "date",defaultValue = "0") int selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        Map uvByCh = trafficChannelStatsService.getUvByCh(selectDate);
        Set chSet = uvByCh.keySet();
        Collection uvCollection = uvByCh.values();

        //json
        return GMVJsonUtil.getUvByChJson(chSet,uvCollection);
    }

    @RequestMapping("/sv")
    public String getSvByCh(@RequestParam(value = "date",defaultValue = "0") int selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        //  List<Bean>  bean: ch, sv -> json
        List<TrafficSvCt> svCtList = trafficChannelStatsService.getSvCt(selectDate);
        return TrafficStatsJsonUtil.selectResultToJson(svCtList,TrafficSvCt.class);
    }


    // 3. 各会话浏览页面数请求拦截方法
    @RequestMapping("/pvPerSession")
    public String getPvPerSession(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = GetDateUtil.getCurrentTime();
        }
        List<TrafficPvPerSession> trafficPvPerSessionList = trafficChannelStatsService.getPvPerSession(date);
        if (trafficPvPerSessionList == null) {
            return "";
        }
        StringBuilder categories = new StringBuilder("[");
        StringBuilder pvPerSessionValues = new StringBuilder("[");

        for (int i = 0; i < trafficPvPerSessionList.size(); i++) {
            TrafficPvPerSession trafficPvPerSession = trafficPvPerSessionList.get(i);
            String ch = trafficPvPerSession.getCh();
            Double pvPerSession = trafficPvPerSession.getPvPerSession();

            categories.append("\"").append(ch).append("\"");
            pvPerSessionValues.append("\"").append(pvPerSession).append("\"");

            if (i < trafficPvPerSessionList.size() - 1) {
                categories.append(",");
                pvPerSessionValues.append(",");
            } else {
                categories.append("]");
                pvPerSessionValues.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"会话平均页面浏览数\",\n" +
                "        \"data\": " + pvPerSessionValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    //渠道平均会话访问时长
    @RequestMapping("/durPerSession")
    public String selectDurPerSession(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = GetDateUtil.getCurrentTime();
        }
        List<TrafficDurPerSession> durPerSession = trafficChannelStatsService.getDurPerSession(date);
        return TrafficStatsJsonUtil.selectResultToJson(durPerSession,TrafficDurPerSession.class);
    }

    //用户跳出率
    @RequestMapping("/ujRate")
    public String selectUjRate(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<TrafficUjRate> ujRate = trafficChannelStatsService.getUjRate(selectDate);
        return TrafficStatsJsonUtil.selectResultToJson(ujRate,TrafficUjRate.class);
    }


    //分时统计
    @RequestMapping("/perHour")
    public String getVisitorPerHr(@RequestParam(value = "date",defaultValue = "20230101") Integer selectDate){
        if (selectDate == 0) {
            selectDate = GetDateUtil.getCurrentTime();
        }

        List<TrafficVisitorStatsPerHour> visitorPerHrStats = trafficVisitorStatsService.getVisitorPerHrStats(selectDate);
        return TrafficStatsJsonUtil.selectVisitorToJson(visitorPerHrStats,TrafficVisitorStatsPerHour.class,0,null);

    }

    //todo 新老访客
    @RequestMapping("userType")
    public String getVisitorTypeStats(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<TrafficVisitorTypeStats> visitorTypeStats = trafficVisitorStatsService.getVisitorTypeStats(selectDate);

        if(visitorTypeStats == null || visitorTypeStats.size()==0){
            return "";
        }
        TrafficVisitorTypeStats newVisitorStats = null;
        TrafficVisitorTypeStats oldVisitorStats = null;
        for (TrafficVisitorTypeStats visitorTypeStat : visitorTypeStats) {
            if("1".equals(visitorTypeStat.getIsNew())){
                newVisitorStats = visitorTypeStat;
            }else {
                oldVisitorStats = visitorTypeStat;
            }
        }
        //
        //拼接json字符串
        String json = "{\"status\":0,\"data\":{\"total\":5," +
                "\"columns\":[" +
                "{\"name\":\"类别\",\"id\":\"type\"}," +
                "{\"name\":\"新访客\",\"id\":\"new\"}," +
                "{\"name\":\"老访客\",\"id\":\"old\"}]," +
                "\"rows\":[" +
                "{\"type\":\"访客数(人)\",\"new\":" + newVisitorStats.getUvCt() + ",\"old\":" + oldVisitorStats.getUvCt() + "}," +
                "{\"type\":\"总访问页面数(次)\",\"new\":" + newVisitorStats.getPvCt() + ",\"old\":" + oldVisitorStats.getPvCt() + "}," +
                "{\"type\":\"跳出率(%)\",\"new\":" + newVisitorStats.getUjRate() + ",\"old\":" + oldVisitorStats.getUjRate() + "}," +
                "{\"type\":\"平均在线时长(秒)\",\"new\":" + newVisitorStats.getAvgDurSum() + ",\"old\":" + oldVisitorStats.getAvgDurSum() + "}," +
                "{\"type\":\"平均访问页面数(人次)\",\"new\":" + newVisitorStats.getAvgPvCt() + ",\"old\":" + oldVisitorStats.getAvgPvCt() + "}]}}";

        return json;
    }

    //todo 关键词统计
    /*
    {
  "status": 0,
  "msg": "",
  "data": [
    {
      "name": "小米",
      "value": 2370
    },
    {
      "name": "ps5",
      "value": 1290
    },
    ...
  ]
}

     */
    @RequestMapping("/keywords")
    public String selectKeywords (@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        List<TrafficKeyWords> trafficKeyWordsList = trafficKeywordService.selectKeywords(selectDate);

        //json
        if(trafficKeyWordsList == null || trafficKeyWordsList.size() == 0){
            return "";
        }
        StringBuilder data = new StringBuilder("[");
        for (int i = 0; i < trafficKeyWordsList.size(); i++) {
            TrafficKeyWords trafficKeyWords = trafficKeyWordsList.get(i);
            String keyWord = trafficKeyWords.getKeyWord();
            Integer keyWordScore = trafficKeyWords.getKeyWordScore();
            data.append("" +
                    "{\n" +
                    "      \"name\": \"" + keyWord + "\",\n" +
                    "      \"value\": " + keyWordScore + "\n" +
                    "    }");
            if(i < trafficKeyWordsList.size()-1){
                data.append(",");
                }else {
                data.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": " + data + "\n" +
                "}";

    }


}
